package es.ats.codington.web.servlets;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import es.ats.codington.festival.domain.Event;
import es.ats.codington.festival.domain.Places;
import es.ats.codington.festival.services.EventDAOService;
import es.ats.codington.festival.services.PlaceDAOService;

/**
 * Servlet implementation class editEventServlet
 */
@WebServlet("/editEventServlet")
public class editEventServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public editEventServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		EventDAOService eventDAO=new EventDAOService();
		
		int eventId=Integer.parseInt(request.getParameter("eventId"));
		String eventName=request.getParameter("eventName");
		String eventDescrip=request.getParameter("eventDescrip");
		int placeId=Integer.parseInt(request.getParameter("place"));
		String duration=request.getParameter("duration");
		String eventType=request.getParameter("eventType");
		String ticket=String.valueOf(request.getParameter("ticket"));
		
		
		PlaceDAOService placeDAOService=new PlaceDAOService();
		
		Places place;
		place=placeDAOService.getPlaceByIdService(placeId);
		Event newEvent = new Event(eventId, eventName, eventDescrip, duration, eventType, ticket, place);
		int i=eventDAO.updateEventService(newEvent);
		
		if(i==1){
			response.sendRedirect("index2.jsp");
		}
		else {
			
			PrintWriter out=response.getWriter();
			out.print("You can not change the event.");
			RequestDispatcher rd= request.getRequestDispatcher("updateEvent.jsp");
			out.close();
		}
	
		
	}
}


